package cn.gov.msa.collection;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import org.xmlpull.v1.XmlPullParserException;

import cn.gov.msa.util.PropertiesTools;
import cn.gov.msa.util.WebServiceTools;
import cn.gov.msa.util.encrypt.BizMsgCrypt;
import net.sf.json.JSONObject;

public class WxService {
	private static Logger logger = Logger.getLogger(WxService.class);
	//申报应用ID
	private static String APP_ID = PropertiesTools.readProperties("wx.appid");
	private static String AESKEY = PropertiesTools.readProperties("wx.aeskey");
	//微信消息通知
	private static String NOTIFY_URL = PropertiesTools.readProperties("wx.inform.url");
	private static String NOTIFY_METHOD = PropertiesTools.readProperties("wx.inform.methods");
	
	public static JSONObject notify(JSONObject notInfo){
		JSONObject wxResp = JSONObject.fromObject("{\"result\":\"0\",\"info\":\"微信发送失败\"}");
		
		try {
			BizMsgCrypt encrypt = new BizMsgCrypt(AESKEY, APP_ID);
			// 加密
			JSONObject encryptMsg = encrypt.encryptMsg(notInfo.toString(), Long.toString(System.currentTimeMillis()), UUID.randomUUID().toString());
			// 调用微信推送
			String result = WebServiceTools.getDataByUrl(NOTIFY_URL, NOTIFY_METHOD, encryptMsg.toString());
			logger.info("wx notify result: " + result);
			wxResp = JSONObject.fromObject(result);
			if (wxResp.getString("result").equals("0")) {
				wxResp.put("result", "1");
			} else {
				wxResp.put("result", "0");
			}
		} catch (Exception e) {
			logger.error("微信推送接口异常.", e);
			wxResp.put("info", "微信推送接口异常." + e.getMessage());
		}
		return wxResp;
	}
}
